﻿Public Class UsrCtrlDiemTrungBinh
    Public Sub LoadCBX()
        Dim DTB_BUS As DiemTrungBinhBUS = New DiemTrungBinhBUS
        Dim sql As String = "Select Distinct(NamHoc) from HOCKY"
        DTB_BUS.LoadCBX(cbxLuaChon, sql)
        cbxLuaChon.DisplayMember = "NamHoc"
        cbxLuaChon.ValueMember = "NamHoc"
    End Sub
    Private _MaHocSinh As String
    Public Property MaHocSinh() As String
        Get
            Return _MaHocSinh
        End Get
        Set(ByVal value As String)
            _MaHocSinh = value
        End Set
    End Property

    Private Sub btnXem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXem.Click
        Dim DTB_BUS As DiemTrungBinhBUS = New DiemTrungBinhBUS
        Dim dtb As DiemTBDTO = New DiemTBDTO
        Dim nh As Integer = cbxLuaChon.SelectedValue
        'lay tb ky 1
        Dim sql As String = "Select kq.TBCuoiKy from KETQUA kq, HOCKY hk where kq.MaHocSinh = '" & MaHocSinh &
                            "' and hk.TenHocKy = 1 and hk.NamHoc = " & nh &
                            " and kq.MaHocKy = hk.MaHocKy"
        Dim tb1 As Double = 0
        Dim GetDT As DataProvider = New DataProvider
        Dim rd As SqlClient.SqlDataReader = GetDT.SQL_ExcuteQueryReader(sql)
        While rd.Read()
            Dim tbmon As Double = rd.GetDouble(0)
            tb1 = tb1 + tbmon
        End While
        sql = "select COUNT(TBCuoiKy) from KETQUA kq, HOCKY hk where kq.MaHocSinh = '" & MaHocSinh &
                    "' and hk.TenHocKy = 1 and hk.NamHoc = " & nh &
                    " and kq.MaHocKy = hk.MaHocKy"
        Dim slmon As Integer = 0
        rd = GetDT.SQL_ExcuteQueryReader(sql)
        While rd.Read()
            slmon = rd.GetInt32(0)
            If slmon <= 0 Then
                slmon = 1
            End If
        End While 'xong tbky1
        dtb.DiemTBKy1 = tb1 / slmon
        'lay tbky2
        sql = "Select kq.TBCuoiKy from KETQUA kq, HOCKY hk where kq.MaHocSinh = '" & MaHocSinh &
                            "' and hk.TenHocKy = 2 and hk.NamHoc = " & nh &
                            " and kq.MaHocKy = hk.MaHocKy"
        Dim tb2 As Double = 0
        While rd.Read()
            Dim tbmon As Double = rd.GetDouble(0)
            tb2 = tb2 + tbmon
        End While
        sql = "select COUNT(TBCuoiKy) from KETQUA kq, HOCKY hk where kq.MaHocSinh = '" & MaHocSinh &
                    "' and hk.TenHocKy = 2 and hk.NamHoc = " & nh &
                    " and kq.MaHocKy = hk.MaHocKy"
        rd = GetDT.SQL_ExcuteQueryReader(sql)
        While rd.Read()
            slmon = rd.GetInt32(0)
            If slmon <= 0 Then
                slmon = 1
            End If
        End While
        rd = GetDT.SQL_ExcuteQueryReader(sql)
        dtb.DiemTBKy2 = tb2 / slmon
        dtb.MaHocSinh = MaHocSinh
        dtb.NamHoc = nh
        DTB_BUS.ThemDTB(dtb)
        sql = "select MaHocSinh, TBKy1, TBKy2, NamHoc from DIEMTRUNGBINH where MaHocSinh = '" & MaHocSinh & "'"
        DTB_BUS.LoadDGV(dgvDTB, sql)
    End Sub

End Class
